<template>
  <div class="m-type-classify-mange">
    <!-- 类型管理 -->
    <div class="add-btn">
      <w-button type="option" @on-click="onSaveUpdate('add', '')">新增</w-button>
    </div>
    <!-- 类型列表 -->
    <div class="m-type-list">
      <w-table :columns="columns" :has-data="hasData">
        <tr v-for="(list, listIndex) in typeList.recordList" :key="listIndex">
          <td>
            <div class="m-options">
              <w-checkbox :label="`list${listIndex}`" v-model="list.checked" @on-change="_setListToAllCheck"></w-checkbox>
              <span class="m-order">{{(paramsInfo.currentPage - 1) * 20 + (listIndex + 1)}}</span>
            </div>
          </td>
          <td>
            <span>{{list.name}}</span>
          </td>
          <td>
            <div class="options">
              <div class="edit" @click="onSaveUpdate('update', list.id)">
                <span class="icon-edit"></span>
                <span>修改</span>
              </div>
              <div class="del" @click="onDeleteList(list.id)">
                <span class="icon-del"></span>
                <span>删除</span>
              </div>
            </div>
          </td>
        </tr>
      </w-table>
    </div>
    <!-- 全选，分页 -->
    <div class="m-all-page clearfix" v-if="typeList.recordList && typeList.recordList.length">
      <div class="fl" style="margin-top: 10px;">
        <w-checkbox label="pageAll" v-model="pageAll" @on-change="onChangePageChecked">本页全选</w-checkbox>
        <w-checkbox label="all" v-model="all" @on-change="onChangeChecked">全选</w-checkbox>
        <span class="delete" @click="onDeleteList('')">删除</span>
      </div>
      <div class="fr" v-if="typeList.pageCount > 1">
        <w-paging :current="typeList.currentPage" :total="typeList.pageCount" @on-page="onChangePage"></w-paging>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { apiClassifyMange } from 'js/api'

export default {
  data() {
    return {
      columns: [
        {title: '序号', width: '8%'},
        {title: '类型名称', width: 'auto'},
        {title: '操作', width: '24%'}
      ],
      pageAll: false, // 本页全选
      all: false, // 全选
      // 公共参数
      paramsCommon: {
        accessToken: this.$store.getters.token || '',
        threeMenuCode: this.$route.query.three || ''
      },
      // 查询分类信息参数
      paramsInfo: {
        currentPage: 1,
        showCount: 20,
        type: 'page'
      },
      typeList: {}
    }
  },
  computed: {
    ...mapGetters(['token']),
    // 判断是否有数据
    hasData() {
      if (!this.typeList.recordList || this.typeList.recordList.length == 0) {
        return false
      } else {
        return true
      }
    }
  },
  created() {
    this.fetchTypeClassify()
  },
  methods: {
    /**
     * 查询商品分类
     */
    fetchTypeClassify() {
      this.paramsInfo = Object.assign({}, this.paramsCommon, this.paramsInfo)
      apiClassifyMange.apiGoodsClassify(this.paramsInfo).then(data => {
        this.typeList = data.data || {}
        // 给每个属性添加checked属性
        if (!this.typeList.recordList || this.typeList.recordList.length == 0) return
        this.typeList.recordList.forEach((v) => {
          this.$set(v, 'checked', this.all)
        })
      })
    },
    /**
     * 分页
     */
    onChangePage(page) {
      if (this.all) {
        this.pageAll = true
      } else {
        this.all = false
        this.pageAll = false
      }
      this.paramsInfo.currentPage = page
      this.fetchTypeClassify()
    },
    /**
     * 本页全选
     */
    onChangePageChecked(bool) {
      // 如果商品列表不存在，则直接抛出
      if (this.typeList.length == 0) {
        return
      }

      // 将当前的商品列表的选中状态和页码的全选按钮一致
      this.typeList.recordList.forEach((v) => {
        v.checked = this.pageAll
      })

      // 如果当前是全部商品全选的，则将本页全选选中
      if (!this.pageAll) {
        this.all = false
      }
    },
    /**
     * 切换全选
     */
    onChangeChecked(bool) {
      if (bool && this.typeList.recordList && this.typeList.recordList.length) {
        this.pageAll = true
        this.typeList.recordList.forEach(list => {
          list.checked = true
        })
      } else {
        this.pageAll = false
        this.typeList.recordList.forEach(list => {
          list.checked = false
        })
      }
    },
    /**
     * 判断选中的个数,是否和展示的长度相同，true相同，则选中全部，false全部取消
     */
    _setListToAllCheck() {
      this.all = true
      this.pageAll = true
      // 如果商品列表中只要有一个是未选中状态，则全选按钮也为未选中状态
      this.typeList.recordList.forEach((v) => {
        if (!v.checked) {
          this.pageAll = false
          this.all = false
        }
      })
      // const list = this.typeList.recordList || {}
      // let aType = list.filter(item => {
      //   return item.checked === true
      // })
      // if (aType.length === list.length) {
      //   this.all = true
      //   this.pageAll = true
      // } else {
      //   this.all = false
      //   this.pageAll = false
      // }
    },
    /**
     * 删除
     */
    onDeleteList(id) {
      let paramsDel = {
        ids: []
      }
      let _this = this
      // 若非全选状态
      if (!this.all) {
        if (!id) {
          this.typeList.recordList.forEach(list => {
            if (list.checked) {
              paramsDel.ids.push(list.id)
            }
          })
        } else {
          paramsDel.ids.push(id)
        }
      }
      paramsDel = Object.assign({}, this.paramsInfo, paramsDel)
      this.$modal({
        title: '提示',
        content: '确认删除？',
        onConfirm() {
          apiClassifyMange.apiGoodsClassifyDelete(paramsDel).then(data => {
            _this.$toast('删除成功', () => {
              _this.fetchTypeClassify()
              _this.all = false
              _this.pageAll = false
            })
          })
        }
      })
    },
    /**
     * 新增，修改
     */
    onSaveUpdate(type, id) {
      if (type == 'add') {
        this.$router.push(`addtype?three=${this.$route.query.three}`)
      } else if (type == 'update') {
        this.$router.push(`addtype?three=${this.$route.query.three}&id=${id}`)
      }
    }
  }
}
</script>

<style lang="scss">
.m-type-classify-mange {
  .add-btn {
    .btn {
      width: 70px;
      height: 28px;
      border: 1px solid #9E9E9E;
      font-size: 12px;
    }
  }
}
.m-type-list {
  .table {
    td {
      height: 50px;
    }
  }
  .m-checkbox .check-btn {
    width: 16px !important;
    height: 16px !important;
  }
}
</style>


<style lang="scss" scoped>
.add-btn {
  margin: 20px 0 10px;
}
.options {
  display: inline-block;
  text-align: center;
  color: #2D8EFE;
  & > div {
    display: inline-block;
    margin: 0 20px;
    cursor: pointer;
  }
  
  .icon-edit {
    width: 12px;
    height: 12px;
    vertical-align: sub;
    background: url(../../assets/icon-edit.png) no-repeat;
    display: inline-block;
    cursor: pointer;
  }
  .icon-del {
    display: inline-block;
    width: 12px;
    height: 12px;
    vertical-align: sub;
    background: url(../../assets/del-bush.png) no-repeat;
    cursor: pointer;
  }
}
.m-type-list {
  .m-options {
    padding-left: 20px;
  }
  .m-order {
    padding-left: 20px;
    display: inline-block;
    width: 160px;
    text-align: left;
  }
}
.m-all-page {
  margin: 30px 0;
  padding-left: 20px;
  .delete {
    padding-left: 20px;
    text-decoration-line: underline;
    cursor: pointer;
  }
}
</style>

